package com.zttek.photosShareProject.modules.web.manager;

import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.json.VO;
import com.zttek.photosShareProject.common.result.AbuseDetailResult;
import com.zttek.photosShareProject.common.result.AbuseResult;
import com.zttek.photosShareProject.modules.model.*;
import com.zttek.photosShareProject.modules.service.login.IUserService;
import com.zttek.photosShareProject.modules.service.login.impl.UserServiceImpl;
import com.zttek.photosShareProject.modules.service.manager.IAbuseService;
import com.zttek.photosShareProject.modules.service.manager.impl.AbuseServiceImpl;
import com.zttek.photosShareProject.modules.service.share.ICommentService;
import com.zttek.photosShareProject.modules.service.share.IImageService;
import com.zttek.photosShareProject.modules.service.share.IShareService;
import com.zttek.photosShareProject.modules.service.share.impl.CommentService;
import com.zttek.photosShareProject.modules.service.share.impl.ImageService;
import com.zttek.photosShareProject.modules.service.share.impl.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 13296 on 2017/8/31.
 */
@Controller
public class AbuseAndExmaineController {
    @Autowired
    private IAbuseService abuseService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IShareService shareService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IImageService imageService;

    @RequestMapping(value = "admin/examineList",method = RequestMethod.GET)
    public String exmaineListView(int select,int pn,Model model,HttpServletRequest request){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }
        List<Abuse> list = null;
        Page<Abuse> page = new Page<>(pn,10);
        try { /*select: -1-非审核，审核0-全部，1-已审核*/
            page =  abuseService.selectAbusePage(page,select);
            list = page.getRecords();
            ArrayList<AbuseResult> abuseList = new ArrayList<>();
            for(Abuse a:list){
                AbuseResult ar = new AbuseResult();
                ar.setId(a.getId());
                ar.setInformTime(a.getCreateTime());
                ar.setTargetId(a.getTargetId());
                ar.setType(a.getType());
                ar.setResult(a.getResult());
                ar.setInformer(userService.selectUserById(a.getInfomerId()).getUsername());
                if(a.getType()==0){
                    Share s = shareService.selectShare(a.getTargetId());
                    ar.setInformant(userService.selectUserById(s.getUserId()).getUsername());
                }else {
                    Comment c = commentService.selectComment(a.getTargetId());
                    ar.setInformant(userService.selectUserById(c.getUserId()).getUsername());
                }
                abuseList.add(ar);
            }
            System.out.println(abuseList);
            model.addAttribute("pages",page.getPages());
            model.addAttribute("abuseList",abuseList);
            model.addAttribute("pn",pn);
            model.addAttribute("select",select);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @RequestMapping(value = "admin/examineList",method = RequestMethod.POST)
    public String exmaineList(int select,int pn,Model model){
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "admin/deleteAbuse",method = RequestMethod.GET)
    public String deleteAbuseView(Long id,Model model){
        System.out.println("delete id:"+id);
        try {
            abuseService.deleteAbuseById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    @RequestMapping(value = "admin/deleteAbuse",method = RequestMethod.POST)
    public String deleteAbuse(int select,int pn,Model model){
        return null;
    }

    @RequestMapping(value = "admin/examineDetail",method = RequestMethod.GET)
    public String examineDetailView(Long abuseId,Model model,HttpServletRequest request){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }
        try {
            Abuse abuse = abuseService.selectAbuseById(abuseId);
            AbuseDetailResult abuseDetailResult = new AbuseDetailResult();
            abuseDetailResult.setAbuseId(abuseId);
            abuseDetailResult.setInformerAccount(userService.selectUserById(abuse.getInfomerId()).getAccount());
            abuseDetailResult.setReason(abuse.getReason());//1
            abuseDetailResult.setResult(abuse.getResult());//2
            User user = null;
            if (abuse.getType()==0){//图片
                Share share = shareService.selectShare(abuse.getTargetId());
                abuseDetailResult.setShareTime(share.getCreateTime());//3
                abuseDetailResult.setDescription(share.getDescription());//4
                Long[] imageIds = VO.divideLongIds(share.getImages());
                ArrayList<String> imagesThumbPath = new ArrayList<>();
                for(Long id:imageIds){
                    imagesThumbPath.add(imageService.selectImageById(id).getThumbPath());
                }
                abuseDetailResult.setImagesThumbPath(imagesThumbPath);//5
                user = userService.selectUserById(share.getUserId());
            }else {//评论
                Comment comment = commentService.selectComment(abuse.getTargetId());
                abuseDetailResult.setCommentContent(comment.getContent());//3
                abuseDetailResult.setCommentTime(comment.getCreateTime());//4
//                abuseDetailResult.setCommentTarget(userService.selectUserById(comment.getTargetId()).getAccount());//5
                user = userService.selectUserById(comment.getUserId());
            }
            abuseDetailResult.setPortraitThumbPath(imageService.selectImageById(user.getPortraitId()).getThumbPath());//6
            abuseDetailResult.setAccount(user.getAccount());//7被举报者账号
            abuseDetailResult.setUsername(user.getUsername());//8被举报者用户名

            model.addAttribute("abuseDetailResult",abuseDetailResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 审核决定最终结果，并发送对应通知
     * @param abuseId
     * @param content
     * @param result
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "admin/dealExamineResult",method = RequestMethod.GET)
    public String examineDetail(Long abuseId, String content, int result, Model model, HttpServletRequest request){
        /*
        * 举报通过：1.通知举报者其举报通过审核；2.通知被举报者其被举报通过原因，并逻辑删除被举报的内容。
        * 举报拒绝：1.通知举报者其举报被撤回，并说明原因。
        */
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "failure1";
        }
        try {
            Abuse abuse = abuseService.selectAbuseById(abuseId);
            abuse.setResult(result);//保存举报审核结果
            /*审核结果保存*/
            Examine examine = new Examine();
            examine.setCreateTime(new Date());//1
            Admin admin = (Admin)request.getSession().getAttribute("currentAdmin");
            examine.setAdminId(admin.getId());//2
            examine.setResult(content);//3
            examine.setTargetId(abuseId);//4
            examine.setFinishTime(new Date());//5

            /*消息通知*/
            Info infoToInformer = new Info();//发送给举报者通知
            Info infoToInformant = new Info();//发送给被举报者通知

            infoToInformer.setToUserId(abuse.getInfomerId());
            infoToInformer.setInteract(0);
            infoToInformer.setTargetId(abuse.getTargetId());
            if(result==1){//举报通过
                infoToInformer.setContent("您的举报已通过审核，我们将对该用户做出相关惩罚处理，感谢您的举报！");
                /*被举报者的消息*/
                User user = userService.selectUserById(abuse.getTargetId());
                infoToInformant.setToUserId(user.getId());
                infoToInformant.setTargetId(abuse.getTargetId());
                infoToInformant.setContent(content);
                infoToInformant.setInteract(0);
            }else if(result==2){//举报拒绝
                infoToInformer.setContent(content);
            }
            abuseService.dealExamine(abuse,examine,infoToInformer,infoToInformant);
            //待处理删除分享也要删除相关数据
        } catch (Exception e) {
            e.printStackTrace();
            return "failure";
        }
        return "success";
    }


}
